﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ilanver.Core.PersistenceSupport;
using ilanver.Core;
using ilanver.Data.NHibernate.Statics;
using NHibernate;

namespace ilanver.Data.NHibernate
{
    public class DbContext : IDbContext
    {
        public string FactoryKey { get; set; }
        public DbContext(string factoryKey)
        {
            Check.Require(!string.IsNullOrWhiteSpace(factoryKey), "factoryKey may not be null or empty");

            FactoryKey = factoryKey;
        }

        private ISession Session
        {
            get
            {
                return NHibernateSession.CurrentFor(FactoryKey);
            }
        }

        /// <summary>
        /// This isn't specific to any one DAO and flushes everything that has been 
        /// changed since the last commit.
        /// </summary>
        public void CommitChanges()
        {
            Session.Flush();
        }

        public IDisposable BeginTransaction()
        {
          return  Session.BeginTransaction();
        }

        public void CommitTransaction()
        {
            Session.Transaction.Commit();
        }

        public void RollbackTransaction()
        {
            Session.Transaction.Rollback();
        }
    }
}
